﻿var comm_id = 0;
var globals = {}
function login_request() {
	$.post("/login/",
		 { login: "John", password: "2pm" },
			function(data){
				alert(data);
			}
	)	
}

function getItem(list, item) {
	var tree = document.getElementById(list);
	var selection = tree.contentView.getItemAtIndex(tree.currentIndex);
  	var id;
  	var name = document.getElementById(item+'_name')
  	var description = document.getElementById(item+'_description')
  	if(selection) {
  		 id = selection.firstChild.firstChild.getAttribute(item+"_id");
  		 comm_id = id;
		}
  	if(id != 0) {
		$.post("/"+item+"/"+id+"/",
		 	{},
			function(data){
				var result = eval(data);
				
				result = result[0];
				
				if (result.status == 'true') {
					
					if(name) name.value = result.data.name;
					
  			 		if(description) description.value = result.data.description;
  			 		common_item_id  = result.id;
  			 		if(result.data.related_item1)
  			 			var related_item1 = $("#related_item1_selector menuitem[ related_item1_id='"+result.data.related_item1+"']")
  			 			if(related_item1)
  			 			document.getElementById('related_item1_selector').selectedIndex = related_item1.attr('index');
  			 			
  			 			
  			 		}
			}
			);
  	} else {
  		if(name) name.value = 'Введите имя';
  		if(description) description.value = 'Введите описание';
  		comm_id  = 0;  		
  	}

}



function saveItem(treeitem, item){

 	var request = {'id': comm_id}
 	var name = document.getElementById(item+'_name')
  	var description = document.getElementById(item+'_description')
  	var related_item1 = $("#related_item1_selector menuitem[selected='true']")
 
  	if(related_item1) request.related_item1 = related_item1.attr('related_item1_id')
  	 
 	if (name) request.name = name.value;
 	if( description) request.description =  description.value; 
	$.post("/"+item+"/save/",
			request,
			function(data){
				
				result = eval(data);
				result = result[0];
				if (result.status == 'true') {
					var tree = document.getElementById(treeitem);
					var child = $("#"+treeitem+" treechildren");
					
					if(comm_id == 0 ) {
									
						var tc = document.createElement('treecell');
						tc.setAttribute('label', result.data.name);
						tc.setAttribute(item+'_id', result.data.id);						
						var tr = document.createElement('treerow');
						var ti = document.createElement('treeitem');
						tr.appendChild(tc);
						ti.appendChild(tr);
						child.append(ti);
					
						
					} else {
						
  						var selection = tree.contentView.getItemAtIndex(tree.currentIndex);
  						selection.firstChild.firstChild.setAttribute('label', result.data.name);
  						
  						}
  				} 				
			}
		);
}

function deleteItem(treeitem, item){
	
		$.post("/"+item+"/delete/",
		 { 'id': comm_id  },
			function(data){
				
				var result = eval(data);
				console.log(result[0])
				if(result[0].status == 'true') {					
					var tree = document.getElementById(treeitem);
					var selection = tree.contentView.getItemAtIndex(tree.currentIndex);
					tree.currentIndex = 1
					selection.parentNode.removeChild(selection);
				}
									 				
			}
		);
 	
}